﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;

namespace ERP_RORZE_PRODUCT.Product.UserControls
{
    public partial class CtrlBusOderCancel : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler Reload;
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        DataTable dt;
        ClsBusOderTable b = new ClsBusOderTable();
        ClsMOTable MO = new ClsMOTable();
        ClsMOOptionTable MOO = new ClsMOOptionTable();
        private LogUtils log;

        public CtrlBusOderCancel()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //kiểm tra xem đã Finish chưa. nếu rồi thì không cho hủy
            if (checkFinish.Checked == true)
            {
                MessageBox.Show("Đơn hàng đã hoàn thành. Không thể hủy!");
            }
            else
            {
                MessageBox.Show("Chắc chắn muốn hủy đơn hàng", "Thông báo", MessageBoxButtons.OKCancel);
                // hủy đơn hàng trong BusOder
                b.BOderNo = cbBusOderNo.Text;
                b.Change = "Cancelled";
                b.ChangeDate = dateUpdateDate.Value;
                b.Finished = true;
                b.Started = true;
                clsBusOderDAO.CancelBusOder(b);

                //write Log
                log.Table = LogConstant.Table.Product.BusOrderList;
                log.Task = LogConstant.Task.Update;
                log.Description = "Cancel BOrderNo: " + b.BOderNo;
                log.WriteLog();

                //Hủy đơn hàng trong MO
                MO.Started = true;
                MO.Finished = true;
                MO.Note = "Cancelled";
                MO.MONo = txtMONo.Text;
                clsBusOderDAO.CancelMO(MO);

                //write Log
                log.Table = LogConstant.Table.Product.MOList;
                log.Task = LogConstant.Task.Update;
                log.Description = "Cancel MONo: " + MO.MONo;
                log.WriteLog();

                // hủy đơn hàng trong MOOption
                MOO.Finished = true;
                MOO.Note = "Cancelled";
                MOO.MONo = txtMONo.Text;
                clsBusOderDAO.CancelMOOption(MOO);

                //write Log
                log.Table = LogConstant.Table.Product.MOOption;
                log.Task = LogConstant.Task.Update;
                log.Description = "Cancel MONo: " + MOO.MONo;
                log.WriteLog();

                CtrlBusOderCancel_Load(sender, e);
                //Reload(sender, e);
                //MessageBox.Show("Đã cập nhật thành công");
                cbBusOderNo.Focus();
                dateUpdateDate.Value = DateTime.Now;
            }
        }

        private void cbBusOderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClsBusOderTable obj = ((ClsBusOderTable)cbBusOderNo.SelectedItem);
            txtPlanNo.Text = obj.PlanNo.ToString();
            txtMONo.Text = obj.MONo.ToString();
            txtChange.Text = obj.Change.ToString();

            checkFinish.Checked = obj.Finished;

            labelControl5.Visible = true;
            labelControl3.Visible = true;
            labelControl4.Visible = true;
            labelControl7.Visible = true;
            txtChange.Visible = true;
            txtMONo.Visible = true;
            txtPlanNo.Visible = true;
            checkFinish.Visible = true;

        }

        private void CtrlBusOderCancel_Load(object sender, EventArgs e)
        {
            dt = clsBusOderDAO.GetAllCancel();
            List<ClsBusOderTable> objs = new List<ClsBusOderTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsBusOderTable obj = new ClsBusOderTable();
                obj.BOderNo = r["BOderNo"].ToString();
                obj.PlanNo = r["planNo"].ToString();
                obj.ChangeDate = (DateTime)r["ChangeDate1"];
                obj.Change = r["Change1"].ToString();
                obj.MONo = r["MONo1"].ToString();
                obj.Finished =  (bool)r["Finished"];
                objs.Add(obj);
            }
            cbBusOderNo.DataSource = objs;
            cbBusOderNo.DisplayMember = "BOderNo";

            dateUpdateDate.Value = DateTime.Now;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void labelControl3_Click(object sender, EventArgs e)
        {

        }

        private void labelControl7_Click(object sender, EventArgs e)
        {

        }
    }
}
